#Developed by Dylan Pavelko
#Created: May 9, 2012
#Last Updated: May 10, 2012

from django.db import models
from fixtures.models import *
from copy import deepcopy
from mptt.models import MPTTModel, TreeForeignKey
from django import forms

class CueSettings(models.Model):
	setting = models.ForeignKey(
		Setting, 
		verbose_name="Device Settings")
		
	def __unicode__(self):
		return self.setting.name
		
	def json(self):
		props = "["
		for i in range(self.setting.properties.count()):
			props = props + self.setting.properties.all()[i].jsonValue() + ","
		
		props = props[:-1]
		props = props + "]"
		return props
		
#	def __unicode__(self):
#		return self.setting
#Property(name="color", value="blue", defaultOffValue="black")
class Transition(models.Model):
	#In the future make this a fixture OR SUB
	fixture = models.ForeignKey(
		Fixture, 
		verbose_name="Fixture")
	seconds = models.DecimalField(max_digits=5, decimal_places=2)
	desiredSettings = models.ForeignKey(
		CueSettings, 
		verbose_name="Desired Settings")

	def __unicode__(self):
		return self.fixture.name + " Transition"
		
	def name(self):
		return self.fixture.name + " Transition"
		
	def json(self):
		return "{\"name\":\"" + str(self.fixture) + "\", \"seconds\":\"" \
		+ str(self.seconds) +"\", \"desiredSettings\":"+self.desiredSettings.json()+"}"

		
#class Cue(models.Model):
#	name = models.CharField(max_length=50)
#	transitions = models.ManyToManyField(
#		Transition, 
#		verbose_name="Transitions")
#		
#	def __unicode__(self):
#		return self.name
#		
#class CueStack(models.Model):
#	name = models.CharField(max_length=50)
#	stack = []
#		
#	def __unicode__(self):
#		return self.name

class Cue(MPTTModel):
	cueName = models.CharField(max_length=50)
	transitions = models.ManyToManyField(
		Transition, 
		verbose_name="Transitions", null=True, blank=True)
	parent = TreeForeignKey('self', null=True, blank=True, related_name='children')
	
	class MPTTMeta:
		order_insertion_by = ['cueName']
		
	def __unicode__(self):
		return self.cueName
		
#	def clean(self):
#		data = self.cleaned_data['parent','transitions']
#		if data.get('parent', None) or data.get('transitions', None):
#			return data
#		else:
#			raise forms.ValidationError('Must only have Parent OR Transitions')